package com.xjscrm.console.service.customer.role.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.SysRole;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.mapper.MerchatChildMapper;
import com.xjscrm.console.mapper.RoleMapper;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.role.SysRoleDto;
import com.xjscrm.console.param.role.SysRoleDtoQuery;
import com.xjscrm.console.param.role.SysRoleIdDto;
import com.xjscrm.console.param.user.ChildMerchatQuery;
import com.xjscrm.console.service.customer.role.RoleService;
import com.xjscrm.console.vo.MerchatChildVo;
import com.xjscrm.console.vo.SysRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MerchatChildMapper merchatChildMapper;

    @Override
    public void addRole(SysRoleDto sysRoleDto) {
        if(sysRoleDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(StringUtils.isBlank(sysRoleDto.getRoleName())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        SysRole sysRole=new SysRole();
        BeanUtil.copyProperties(sysRoleDto,sysRole);
        roleMapper.addRole(sysRole);
    }

    @Override
    public void updateRole(SysRoleDto sysRoleDto) {
        if(sysRoleDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(sysRoleDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
         SysRole sysRole= roleMapper.getById(sysRoleDto.getId(),sysRoleDto.getMerId());

        if(sysRole!=null){
            if(!StringUtils.isBlank(sysRoleDto.getRoleName())){
                sysRole.setRoleName(sysRoleDto.getRoleName());
            }
            if(!StringUtils.isBlank(sysRoleDto.getAuthData())){
                sysRole.setAuthData(sysRoleDto.getAuthData());
            }
            roleMapper.updateSysRole(sysRole);
        }
    }


    @Override
    public Object getRoleList(SysRoleDtoQuery sysRoleDtoQuery) {
        if(sysRoleDtoQuery.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
         List<SysRoleVo> sysRoleVoList=roleMapper.getRoleList(sysRoleDtoQuery);
        return sysRoleVoList;
    }

    @Override
    public Object getRoleDropdwon(BaseDto baseDto) {
        if(baseDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        SysRoleDtoQuery sysRoleDtoQuery=new SysRoleDtoQuery();
        sysRoleDtoQuery.setMerId(baseDto.getMerId());
        List<SysRoleVo> sysRoleVoList=roleMapper.getRoleList(sysRoleDtoQuery);
        ArrayList<Map> mapArrayList = new ArrayList<>();
        if(sysRoleVoList!=null&&!sysRoleVoList.isEmpty()){
            for (SysRoleVo sysRoleVo:sysRoleVoList) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("value",sysRoleVo.getId());
                map.put("label",sysRoleVo.getRoleName());
                mapArrayList.add(map);
            }
        }
        return mapArrayList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleRole(SysRoleDto sysRoleDto) {
        if(sysRoleDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(sysRoleDto.getId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.ID_NULL);
        }
        //查询所有使用该角色的子账号
        ChildMerchatQuery childMerchatQuery=new ChildMerchatQuery();
        childMerchatQuery.setMerId(sysRoleDto.getMerId());
        childMerchatQuery.setRoleId(sysRoleDto.getId());
        List<MerchatChildVo> merchatChildVoList = merchatChildMapper.getMerchatChild(childMerchatQuery);
        if(merchatChildVoList!=null&&!merchatChildVoList.isEmpty()){
            for (MerchatChildVo merchatChildVo:merchatChildVoList) {
                merchatChildMapper.deleRoleIdById(merchatChildVo.getId(),merchatChildVo.getMerId());
            }
        }
        roleMapper.deleSysRoleById(sysRoleDto.getId(),sysRoleDto.getMerId());
    }

    @Override
    public Object findSysRoleInfo(SysRoleIdDto sysRoleIdDto) {
        if (sysRoleIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        SysRole sysRole = roleMapper.getById(sysRoleIdDto.getId(), sysRoleIdDto.getMerId());

        if (sysRole == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该角色不存在");
        }

        SysRoleVo sysRoleVo = new SysRoleVo();
        BeanUtils.copyProperties(sysRole, sysRoleVo);

        return sysRoleVo;
    }

    @Override
    public SysRole selectById(Long roleId, @MerId Long merId) {

        SysRole sysRole = roleMapper.getById(roleId, merId);

        if (sysRole == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该角色不存在");
        }
        return sysRole;
    }
}
